#!/usr/bin/env jq
# compile-code-dataflow_dot -- Generates dependency graph of processes in Graphviz DOT language
##

def nodeId: sub("/"; "/\n");
def nodeType: sub("/.*$"; "");

# produce all prefixes (longest to shortest, being empty)
def nodeTypes: split("/") | range(length;0;-1) as $i | .[:$i] | join("/");

(.deepdive_.execution.dot // {}) as $deepdiveDotConfig |

# Graphviz attrs (See: http://www.graphviz.org/doc/info/attrs.html#k:style)
# graph attributes
(
    "rankdir=BT  # bottom-to-top drawing (or RL/right-to-left may also make sense)
    \($deepdiveDotConfig.graph_attrs // "")"
) as $graph_attrs |
# node attributes by [type]
(
    { ""       : "shape=box
                 ,style=dotted
                 ,fontsize=10
                 ,fontname=\"Ubuntu Mono,Envy Code R,Oxygen Mono,Consolas,Menlo,Monaco,Andale Mono\"
                 "
    , data     : "shape=box3d    color=\"#2222cc\" style=solid"
    , process  : "shape=ellipse  color=\"#cc2222\" style=solid"
    , model    : "shape=septagon color=\"#ffffff\" style=filled fillcolor=\"#cc22cc\""
    } * ($deepdiveDotConfig.node_attrs // {})
) as $node_attrs |
# edge attributes by [srcType][dstType]
(
    { ""       : { "": "color=\"#999999\"" }
    } * ($deepdiveDotConfig.edge_attrs // {})
) as $edge_attrs |

.deepdive_.execution.dependencies |
{ path: "dataflow.dot", content: [
"
digraph \"\(env.DEEPDIVE_APP // "" | sub(".*/"; "")) data flow\" {
    graph[\($graph_attrs)
    ];
    node [\(try ($node_attrs[""]     // "") catch "")
    ];
    edge [\(try ($edge_attrs[""][""] // "") catch "")
    ];

", ( to_entries[]
# process and data nodes
| "
\"\(.key | nodeId)\" [\([$node_attrs[.key | nodeTypes] | select(.)] | first // "")];
"

# dependency edges
, ( { from: .value[], to: .key } | "
\"\(.from | nodeId)\" -> \"\(.to | nodeId)\" [
    label=\"\"
    \([ ($edge_attrs   [.from | nodeTypes] | select(.)) as $edge_attrs_from
      |  $edge_attrs_from[.to | nodeTypes] | select(.)
      ] | first // "" # TODO don't repeat $edge_attrs[""][""]
      )
];
" )

), "

}
"
] | join("") }
